package com.zr.task.presenter;

import android.content.Context;

import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.model.TaskModel;
import com.zh.baselibrary.netutils.BaseResp;
import com.zh.baselibrary.rxjava.RxSubscribe;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.Errata;
import com.zh.model.task.error.ErrorDetailBean;
import com.zh.model.task.error.ErrorDetailVOBean;
import com.zh.model.task.home.ClassCabinetBean;
import com.zr.andealib.util.HardEvent;
import com.zr.andealib.util.LockUtil;
import com.zr.andealib.util.constant.AndeaConstant;
import com.zr.task.presenter.view.CorrigendumDetailView;
import com.zr.task.utils.EquipmentTypeUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.Observable;

public class CorrigedumPresenter extends BasePresenter<CorrigendumDetailView> {
    private TaskModel model;
    private int id;
    private String schoolId;


    @Override
    public void attachModel(Context context) {
        model = new TaskModel(context);
        schoolId = ShareManager.getValue(context, BaseConstant.DEVICE_SCHOOL_ID);
    }

    public void getErrorDetail(ErrorDetailVOBean mBean) {
        id = mBean.getStudentId();
        Observable<BaseResp<ErrorDetailBean>> observable = getObservable(model.errorDetail(mBean));
        observable.subscribe(new RxSubscribe<BaseResp<ErrorDetailBean>>() {
            @Override
            public void onSuccess(BaseResp<ErrorDetailBean> errorDetailBeanBaseResp) throws Exception {
                getView().detailSuccess(errorDetailBeanBaseResp.getData());
            }

            @Override
            public void onHint(String hint) {
                getView().detailError(hint);
            }
        });

    }

    public void complte(ErrorDetailVOBean versionBean) {
        Observable<BaseResp<Object>> observable = getObservableNotStop(model.errorComplte(versionBean));
        observable.subscribe(new RxSubscribe<BaseResp<Object>>() {
            @Override
            public void onSuccess(BaseResp<Object> objectBaseResp) throws Exception {
                if (objectBaseResp.getCode() == 0) {
                    getView().complteSuccess();
                } else {
                    ToastView.showLong(objectBaseResp.getMsg());
                }
            }

            @Override
            public void onHint(String hint) {
                ToastView.showLong(hint);
            }
        });
    }
    public void openSuccess(){
        Observable<BaseResp<Object>> observable = getObservable(model.openSuccess(new Errata(schoolId,2,id+"")));
        observable.subscribe(new RxSubscribe<BaseResp<Object>>() {
            @Override
            public void onSuccess(BaseResp<Object> objectBaseResp) throws Exception {
            }
            @Override
            public void onHint(String hint) {
            }
        });
    }
    Timer timer = new Timer();

    public void openDoor(ErrorDetailVOBean versionBean) {
        LockUtil lockUtil = new LockUtil();
        lockUtil.init();
        int boxNum = softToHardNum(versionBean.getCabinetNum());
        lockUtil.openDoor(boxNum);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                int status = lockUtil.readLock(boxNum);

                if (status == 1) {
                    timer.cancel();
                    timer.purge();
                    timer = null;
                    LogUtils.e("FASONG ");
                    EventBus.getDefault().post(new HardEvent(boxNum, AndeaConstant.CALL_CORRIGEDUM));
                    if (mViewRef != null && getView() != null)
                        complte(versionBean);

                }


            }
        }, 0, 100);
    }



    private int[] cabinetNumToDevice8N = {0, 1, 6, 2, 4, 7, 3, 5, 8};
    private int[] cabinetNumToDevice11N = {0, 1, 6, 9, 2, 4, 7, 10, 3, 5, 8, 11};

    private int softToHardNum(int boxNum) {
        int totalNum = EquipmentTypeUtils.getBoxNum(getContext());
        if (boxNum <= totalNum) {
            if (totalNum <= 2)
                return boxNum;
            else if (totalNum == 8)
                return cabinetNumToDevice8N[boxNum];
            else
                return cabinetNumToDevice11N[boxNum];
        } else {
            ToastView.showLong("物理编号与界面编号对应不上");
            return boxNum;
        }
    }
}
